package SOSO;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
import java.util.Set;

public class Menu {
    MobileCard mobileCard = new MobileCard();

    public boolean menu(String cardNumber, String password) {
        boolean index = false;
        Set<String> set = Test.mobileCards.keySet();
        for (String s : set) {
            if (s.equals(cardNumber) && Test.mobileCards.get(s).getPassWord().equals(password)) {
                index = true;
                System.out.println("***用户菜单***");
                System.out.println("1、本月账单查询");
                System.out.println("2、套餐余量查询");
                System.out.println("3、打印消费详单");
                System.out.println("4、套餐更改");
                System.out.println("5、办理退网");

            }
        }
        return index;
    }

    public void showAmounDetail(String cardNumber) {
        System.out.println("***本月账单查询***");
        System.out.println("您的卡号是：" + cardNumber);
        System.out.println("当月账单：");
        System.out.println("套餐资费：" + Test.mobileCards.get(cardNumber).getSerPackage().getPrice());
        System.out.println("本月其他消费：" + String.format("%.2f",Test.mobileCards.get(cardNumber).getConsumAmount()));
        System.out.println("账户余额：" + String.format("%.2f",Test.mobileCards.get(cardNumber).getMoney()));
    }

    public void showRemainDetail(MobileCard mobileCard) {
        int talkTime = 0;
        int sendTime = 0;
        double flow = 0;
        System.out.println("***套餐余量查询***");
        System.out.println("您的卡号是：" + mobileCard.getCardNumber());
        if (mobileCard.getSerPackage() instanceof TalkPackage) {
            TalkPackage talkPackage = (TalkPackage) mobileCard.getSerPackage();
            talkTime = talkPackage.getTalkTime() - mobileCard.getRealTalkTime();
            sendTime = talkPackage.getSmsCount() - mobileCard.getRealSMSCount();
            if (sendTime < 0) {
                sendTime = 0;
            }
            if (talkTime < 0) {
                talkTime = 0;
            }
        } else if (mobileCard.getSerPackage() instanceof NetPackage) {
            NetPackage netPackage = (NetPackage) mobileCard.getSerPackage();
            flow = netPackage.getFlow() - mobileCard.getRealFlow();
            if (flow < 0) {
                flow = 0;
            }
        } else if (mobileCard.getSerPackage() instanceof SuperPackage) {
            SuperPackage superPackage = (SuperPackage) mobileCard.getSerPackage();
            talkTime = superPackage.getTalkTime() - mobileCard.getRealTalkTime();
            sendTime = superPackage.getSmsCount() - mobileCard.getRealSMSCount();
            flow = superPackage.getFlow() - mobileCard.getRealFlow();
            if (flow < 0) {
                flow = 0;
            }
            if (sendTime < 0) {
                sendTime = 0;
            }
            if (talkTime < 0) {
                talkTime = 0;
            }
        }
        System.out.println("通话时长：" + talkTime + "分钟");
        System.out.println("短信条数：" + sendTime + "条");
        System.out.println("上网流量：" + flow / 1024 + "GB");

    }

    public void printConsumInfo(String number) {
        Writer writer = null;
        BufferedWriter bw = null;
        try {
            writer = new FileWriter("f:/soso/" + number + "的消费记录.txt", true);
            bw = new BufferedWriter(writer);
            bw.write("********" + number + "的消费记录********\n");
            bw.write("序号\t\t类型\t\t数据(通话（分钟） 上网（MB） 短信（条）\n");

        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean index = false;
        Set<String> set = Test.consumInfos.keySet();
        for (String s : set) {
            if (s.equals(number)) {
                int i = 1;
                for (ConsumInfo consumInfo : Test.consumInfos.get(s)) {
                    if (!consumInfo.getType().equals(null)) {
                        try {
                            bw.write(i + "\t\t" + consumInfo.getType() + "\t\t" + consumInfo.getConsumData()+"\n");
                            index = true;
                            i++;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                try {
                    bw.close();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (!index) {
            System.out.println("对不起，消费记录为空！");
        } else {
            System.out.println("打印成功！");
        }

    }

    public void changePackage(String number) {
        ServicePackage servicePackage = null;
        Scanner input = new Scanner(System.in);
        System.out.println("请问需要换成什么套餐？");
        if (Test.mobileCards.get(number).getSerPackage() instanceof TalkPackage) {
            System.out.println("1、网虫套餐");
            System.out.println("2、超人套餐");
            System.out.println("请选择：");
            if (input.nextInt() == 1) {
                Test.mobileCards.get(number).setSerPackage(servicePackage = new NetPackage());
            } else {
                Test.mobileCards.get(number).setSerPackage(servicePackage = new SuperPackage());
            }
        } else if (Test.mobileCards.get(number).getSerPackage() instanceof NetPackage) {
            System.out.println("1、话痨套餐");
            System.out.println("2、超人套餐");
            System.out.println("请选择：");
            if (input.nextInt() == 1) {
                Test.mobileCards.get(number).setSerPackage(servicePackage = new TalkPackage());
            } else {
                Test.mobileCards.get(number).setSerPackage(servicePackage = new SuperPackage());
            }
        } else if (Test.mobileCards.get(number).getSerPackage() instanceof SuperPackage) {
            System.out.println("1、话痨套餐");
            System.out.println("2、网虫套餐");
            System.out.println("请选择：");
            if (input.nextInt() == 1) {
                Test.mobileCards.get(number).setSerPackage(servicePackage = new TalkPackage());
            } else {
                Test.mobileCards.get(number).setSerPackage(servicePackage = new NetPackage());
            }
        }
        servicePackage.showInfo();
        System.out.println("更换成功！资费下月生效");
    }

    public void remove(String number) {
        Scanner input = new Scanner(System.in);
        System.out.println("确定退网吗？(y/n)");
        if (input.next().equals("y")) {
            Test.mobileCards.remove(number);
            System.out.println("退网成功！");
        }
    }
}
